package com.hw.backapi.listener;

import com.hw.backapi.event.LogEvent;
import com.hw.model.entity.AdminLogPO;
import com.hw.model.mapper.AdminLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author lipeng
 * @description 日志监听
 * @date 2023/3/7 10:32
 */
@Component
public class LogListener implements ApplicationListener<LogEvent> {

    private AdminLogMapper adminLogMapper;

    public LogListener(AdminLogMapper adminLogMapper) {
        this.adminLogMapper = adminLogMapper;
    }

    @Override
    @Async
    public void onApplicationEvent(LogEvent event) {
        AdminLogPO adminlog = event.getAdminlog();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        adminLogMapper.insert(adminlog);
    }
}
